Thread: delete and delete []

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    69

    delete and delete []

    What's exactly the difference between delete and delete [] ?

    I read that delete [] is used when deleting an array,because it deletes all elements in the array and the array itself

    And if i do this ??

    Code:
    char  c = new char[20];
    
    delete c; // instead of    delete [] c
    is it an error ?? compilers never report errors in these cases,but
    is the array really being deleted?


    I tried also to create an array of pointers to objects and verify that delete []
    calls actually the destructors of all objects in the array before
    deleting the array itself

    In order to do that i put a printf in the class' destructor,but it wasn't displayed

    Code:
    SomeClass  **objectptr = new SomeClass*[20];
    
     // create the objects
    
    for(int i = 0;i < 20;i++)
     objectptr[i] = new SomeClass();
    
    //..
    
    delete [] objectptr;  // this doesn't delete the objects...but just
                                    // the memory occupied by their pointers
    How do you use delete [] ?
    Last edited by Lionel; 05-18-2005 at 04:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. delete []
    By homeyg in forum C++ Programming
    Replies: 2
    Last Post: 06-02-2005, 06:21 PM
  2. delete []
    By Zahl in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2002, 08:11 AM
  3. Which comes first...delete [] or NULL?
    By Waldo2k2 in forum C++ Programming
    Replies: 13
    Last Post: 08-09-2002, 09:05 AM
  4. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM
  5. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 05:50 PM